package com.kitkat.serviceImpl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.kitkat.dao.AbstractTokenDAO;
import com.kitkat.dao.UserDAO;
import com.kitkat.entities.User;
import com.kitkat.entities.UserToken;
import com.kitkat.message.ActiveAccountMessage;
import com.kitkat.message.Message;
import com.kitkat.service.UserService;
import com.kitkat.util.MD5;
import com.kitkat.util.MyUtil;

@Component
public class UserServiceImpl implements UserService{
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private AbstractTokenDAO<String, UserToken> userTokenDAO;
	@Autowired
	private JmsTemplate jmsTemplate;
	
	@Override
	@Transactional
	public Map<String,Object> registerUser(String email, String password) {
		Map<String,Object> hm = new HashMap<String,Object>();
		hm.put("code", 200);
		User user = this.userDAO.getByEmail(email);
		if(user!=null){
			hm.put("message", "该邮箱已被注册!");
			hm.put("result", null);
			return hm;
		}
		String temp = email + password + Calendar.getInstance().toString();
		String activeCode = MD5.hash(temp);
		user = new User(email,password,activeCode);
		this.userDAO.saveUser(user);
		
		String href = MyUtil.getBasePath()+"/user/active?email="+email+"&activeCode="+activeCode;
		final Message message = new ActiveAccountMessage(email, href);
		jmsTemplate.send(new MessageCreator() {
			@Override
			public javax.jms.Message createMessage(Session session) throws JMSException {
				return session.createObjectMessage(message);
			}
		});
		hm.put("message", "success");
		hm.put("result", null);
		return hm;
	}

	@Override
	@Transactional
	public Map<String,Object> userLogin(String email, String password) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("code", 200);
		
		String token = MD5.hash(email);
		UserToken userToken = this.userTokenDAO.get(token);
		
		if(userToken!=null&&userToken.getUser().getPassword().equals(password)){
			map.put("message", "success");
			map.put("result", token);
			userToken.setLastModifyDate(new Date());
			this.userTokenDAO.set(token, userToken);
			return map;
		}
		
		User user = this.userDAO.getByEmail(email);
		if(user==null){
			map.put("message", "用户不存在！");
			map.put("result", null);
			return map;
		}
		if(!password.equals(user.getPassword())){
			map.put("message", "邮箱和密码不匹配！");
			map.put("result", null);
			return map;
		}
		if(!user.isActived()){
			map.put("message", "账号尚未激活！");
			map.put("result", null);
			return map;
		}
		map.put("message", "success");
		userToken = new UserToken(user, new Date());
		this.userTokenDAO.set(token, userToken);
		map.put("result", token);
		return map;
	}

	@Override
	@Transactional
	public String activeUser(String email, String activeCode) {
		User user = this.userDAO.getByEmail(email);
		if(user==null){
			return "该用户不存在！";
		}
		if(user.isActived()){
			return "用户已经激活！";
		}
		if(!user.getActiveCode().equals(activeCode)){
			return "激活码错误！";
		}
		user.setActived(true);
		user.setActiveCode(MD5.hash(Calendar.getInstance().toString()));
		this.userDAO.updateUser(user);
		return "success";
	}
}
